//
//  ForwardToFriedsController.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/4/16.
//  Copyright © 2016年 game. All rights reserved.

#import "ForwardToFriedsController.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import "CGFdbmBase.h"
#import "WXApi.h"
#import "WeiboSDK.h"
#import "PageModels.h"
#import "ShareModel.h"
CG_INLINE void AlertLog (NSString *titleStr,NSString *message,NSString *button1,NSString *button2)
{
    if(!titleStr)
        titleStr = @"";
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: titleStr
                                                        message: message
                                                       delegate: nil
                                              cancelButtonTitle: button1
                                              otherButtonTitles: button2,
                              nil];
    [alertView show];
    
}

@interface ForwardToFriedsController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *wChatOffe;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *wboOffe;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *wwww;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *hhhh;
@property (nonatomic,strong) PageModels *pageModel;
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation ForwardToFriedsController
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray= [NSMutableArray array];
    }
    return _dataArray;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.app.myTabBar.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    StatusBlack;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.frame = [UIScreen mainScreen].bounds;
    self.wChatOffe.constant = -(SCREEN_WIDTH)/5;
    self.wboOffe.constant = (SCREEN_WIDTH)/5;
    
    self.wwww.constant = SCREEN_HEIGHT*0.08;
    self.hhhh.constant = self.wwww.constant;
    
    [CGFdbmBase OpenSQlWhiPathName:@"FirstPage.db"];
    NSInteger TableCount = [CGFdbmBase getTableItemCount:@"FirstPage"];
    
    if (TableCount!=0) {
        [self.dataArray addObjectsFromArray:[CGFdbmBase ListWhith:@"FirstPage" dicName:@"Dic"]];
        self.pageModel = [[PageModels alloc]initWithDictionary:_dataArray.firstObject error:nil];
    }

}
- (IBAction)backAction:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)fried:(id)sender {
    
    if ([WXApi isWXAppInstalled]) {
        
         [self ShareWhithSSDKPlatformType:SSDKPlatformSubTypeWechatTimeline];
    }
    else{
        AlertLog(@"", @"没有安装微信客户端，无法进行朋友圈分享", @"确定", nil);
        
    }
}

- (IBAction)Qzoze:(id)sender {
    
    if ([QQApiInterface isQQInstalled]) {
        [self ShareWhithSSDKPlatformType:SSDKPlatformSubTypeQZone];
    }
    else{
        AlertLog(@"", @"没有安装QQ客户端，无法进行空间分享", @"确定", nil);
    }
}


- (IBAction)wchat:(id)sender {

    if ([WXApi isWXAppInstalled]) {
        
        [self ShareWhithSSDKPlatformType:SSDKPlatformSubTypeWechatSession];
    }
    else{
        AlertLog(@"", @"没有安装微信客户端，无法进行微信分享", @"确定", nil);
        
    }

}

- (IBAction)qq:(id)sender {
    
    if ([QQApiInterface isQQInstalled]) {
        
        [self ShareWhithSSDKPlatformType:SSDKPlatformSubTypeQQFriend];
        
        
    }else{
        AlertLog(@"", @"没有安装QQ客户端，无法进行QQ分享", @"确定", nil);
    }
}

- (IBAction)wbo:(id)sender {
    
    if ([WeiboSDK isCanShareInWeiboAPP]) {
        
        [self ShareWhithSSDKPlatformType:SSDKPlatformTypeSinaWeibo];

    }
    else{
        AlertLog(@"", @"没有安装微博客户端，无法进行微博分享", @"确定", nil);
        
    }
}


-(void)ShareWhithSSDKPlatformType:(NSInteger)SSDKPlatformType{
    
    ShareModel *shareModel = self.pageModel.share;
    if (shareModel!=nil) {
        
    NSString *utf8String = shareModel.share_url;
    NSString *title = shareModel.share_title;
    NSString *descriotion = shareModel.share_text;
    NSString *previewImageUrl = shareModel.share_img;
    
    NSArray* imageArray =@[previewImageUrl];
    if (imageArray) {
        
        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        [shareParams SSDKEnableUseClientShare];
        [shareParams SSDKSetupShareParamsByText:descriotion
                                         images:imageArray
                                            url:[NSURL URLWithString:utf8String]
                                          title:title
                                           type:SSDKContentTypeWebPage];
        
        [ShareSDK share:SSDKPlatformType
             parameters:shareParams
         onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
             
         }];
        
    }
    }else{
    
        [Manager GetHudWhithText:@"暂时还不能分享" AndFatherView:self.view];
    }
}


@end